<html>
<head>
  <title>Loans - Partner</title>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <link rel="stylesheet" type="text/css" href="mystyle.css"/>
  <!--Load the AJAX API-->
  <script type="text/javascript" src="http://www.google.com/jsapi"></script>
  <script type="text/javascript">
    // Load the Visualization API and the piechart package.
    google.load('visualization', '1', {'packages':['table']});
    google.setOnLoadCallback(drawTable);

    function drawTable() {
      var data = new google.visualization.DataTable(
      {
        cols:
        [
          {id: 'ID', label: 'ID', type: 'number'},
          {id: 'NAME', label: 'Name', type: 'string'},
          {id: 'STATUS', label: 'Status', type: 'string'},
          {id: 'RATING', label: 'Rating', type: 'number'},
          {id: 'STARTDATE', label: 'Start Date', type: 'date'},
          {id: 'DELINQUENCYRATE', label: 'Delinquency Rate', type: 'number'},
          {id: 'DEFAULTRATE', label: 'Default Rate', type: 'number'},
          {id: 'TOTALRAISED', label: 'Total Raised', type: 'number'},
          {id: '#LOANS', label: '# Loans', type: 'number'},
          {id: 'COUNRTRIES', label: 'Countries', type: 'string'}
        ],
        rows:
        [
/*
  {c:[{v:129},{v:'Tanaoba Lais Manekat Foundation (TLM)'},{v:'pilot'},{v:2.5},{v:new Date(2009,03,12)},{v:0},{v:0},{v:9850},{v:4},{v:'Indonesia'}]}
,{c:[{v:128},{v:'Hagdan sa Pag-uswag Foundation, Inc. (HSPFI)'},{v:'pilot'},{v:2},{v:new Date(2009,02,17)},{v:0},{v:0},{v:25650},{v:112},{v:'Philippines'}]}
,{c:[{v:127},{v:'EDESA'},{v:'pilot'},{v:2.5},{v:new Date(2009,02,10)},{v:0},{v:0},{v:10575},{v:11},{v:'Costa Rica'}]}
*/
          <?php
            $ch = curl_init();
            curl_setopt ($ch, CURLOPT_URL, 'http://api.kivaws.org/v1/partners.xml&app_id=org.kivadata');
            curl_setopt($ch, CURLOPT_HEADER, 0); 
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //don't display information on page
            $xml = curl_exec($ch); 
            $dom = new DOMDocument();
            $dom->loadXML($xml); 
            $partners = $dom->getElementsByTagname('partner');
          
            $partnerIndex = 1;
            foreach ($partners as $partner)
            {
              $nodes = $partner->getElementsByTagname('id'); $node = $nodes->item(0); $id = $node->nodeValue;
              $nodes = $partner->getElementsByTagname('name'); $node = $nodes->item(0); $name = $node->nodeValue;
              //Escape apostrophes
              $name = str_replace("'", "\'", $name);
              $nodes = $partner->getElementsByTagname('status'); $node = $nodes->item(0); $status = $node->nodeValue;
              $nodes = $partner->getElementsByTagname('rating'); $node = $nodes->item(0); $rating = $node->nodeValue;
              $nodes = $partner->getElementsByTagname('start_date'); $node = $nodes->item(0); $start_date = $node->nodeValue;
              //Convert this format: 2006-02-15T18:00:00Z to this: new Date(2008, 1, 28, 0, 31, 26)
              $start_date = "new Date(".substr($start_date,0,4).",".substr($start_date,5,2).",".substr($start_date,8,2).")";
              $nodes = $partner->getElementsByTagname('delinquency_rate'); $node = $nodes->item(0); $delinquency_rate = $node->nodeValue;
              $nodes = $partner->getElementsByTagname('default_rate'); $node = $nodes->item(0); $default_rate = $node->nodeValue;
              $nodes = $partner->getElementsByTagname('total_amount_raised'); $node = $nodes->item(0); $total_amount_raised = $node->nodeValue;
              $nodes = $partner->getElementsByTagname('loans_posted'); $node = $nodes->item(0); $loans_posted = $node->nodeValue;
        
              //TODO: Countries
              $countryNames = "";
              $countries = $partner->getElementsByTagName('country');
              foreach ($countries as $country)
              {
                $nodes = $country->getElementsByTagname('name'); $node = $nodes->item(0); $countryNames = $countryNames.",".$node->nodeValue;
                //Escape apostrophes
                $countryNames = str_replace("'", "\'", $countryNames);
              }
              $countryNames = substr($countryNames, 1); //remove first comma
              
              //Some values may be null.
              if($rating == '') {$rating = 'null';}
              if($delinquency_rate == ''){$delinquency_rate = 'null';}
              if($default_rate == ''){$default_rate = 'null';}
              if($total_amount_raised == ''){$total_amount_raised = 'null';}


              //Put comma in front, except for first item
              if($partnerIndex > 1)
              {
                echo ",";
              }
              $partnerIndex = $partnerIndex+1;
              
          
              echo "{c:[{v:$id},{v:'$name'},{v:'$status'},{v:$rating},{v:$start_date},";
              echo "{v:$delinquency_rate},{v:$default_rate},{v:$total_amount_raised},{v:$loans_posted},{v:'$countryNames'}]}\n";
            }
          ?>
        ]
      });

      var table = new google.visualization.Table(document.getElementById('partner'));

      var formatter = new google.visualization.TableNumberFormat({suffix: '%', fractionDigits: 2});
      formatter.format(data, 5);
      formatter = new google.visualization.TableNumberFormat({suffix: '%', fractionDigits: 2});
      formatter.format(data, 6);
      formatter = new google.visualization.TableNumberFormat({prefix: '$', fractionDigits: 0});
      formatter.format(data, 7);
      formatter = new google.visualization.TableNumberFormat({fractionDigits: 0});
      formatter.format(data, 8);

      //important: set allowHtml to true!
      table.draw(data, {allowHtml: true, showRowNumber: true});
    }
  </script>
  <script type="text/javascript" src="jquery.js"></script>
  <script type="text/javascript" src="menu.js"></script>
  <script type="text/javascript">
    $(document).ready(function() { buildMenus(); });
  </script>
  <script type="text/javascript">var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));</script>
  <script type="text/javascript">try {var pageTracker = _gat._getTracker("UA-7674244-2");pageTracker._trackPageview();} catch(err) {}</script>
</head>
<body>


<div id="content">
  <div id="partner"></div>
</div>
<div id="menu"></div>

</body>
</html>
